package com.flashcash.solu.common.network;

import android.util.Pair;

import com.flashcash.kotlin.data.FileType;
import com.flashcash.kotlin.data.OcrResultBean;
import com.x.leo.apphelper.log.XLog;

import org.jetbrains.annotations.Nullable;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.adapter.rxjava.HttpException;
import rx.Observable;

/**
 * Created by localuser on 2017/3/1.
 */

public class FileUploadUtil {
    public static Observable<Pair<Call<ResponseBody>, Response<ResponseBody>>> uploadPhotoFile
            (FileType fileType, File file, String token) {
        MediaType mediaType;
        mediaType = MediaType.parse(FileUtils.getMimeType(file));
        // create RequestBody instance from file
        RequestBody requestFile = RequestBody.create(mediaType, file);

        // MultipartBody.Part is used to send also the actual file name
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(),
                requestFile);

        //        RequestBody fileType = RequestBody.create(MultipartBody.FORM, FileType
        // .KTP_PHOTO.name().getBytes());

        // finally, execute the request

        final FileUploadApi service = ServiceGenerator.createService(FileUploadApi.class);
        final Call<ResponseBody> call = service.uploadPhoto(body, fileType.name(), token);

        return Observable.create(subscriber -> call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call1,
                                   Response<ResponseBody> response) {
                XLog.INSTANCE.i("Upload success", 10);
                if (response.isSuccessful()) {
                    subscriber.onNext(new Pair<>(call1, response));
                } else {
                    subscriber.onError(new HttpException(response));

                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call1, Throwable t) {
                XLog.INSTANCE.e("Upload error:", t, 100);
                subscriber.onError(new UploadFileException(call1, t));

            }
        }));
    }

    private static Observable<Pair<Call<ResponseBody>, Response<ResponseBody>>> uploadVideoFile
            (FileType fileType, File file, String token, String loanAppId) {

        MediaType mediaType = MediaType.parse(FileUtils.getMimeType(file));
        RequestBody requestFile = RequestBody.create(mediaType, file);
        // MultipartBody.Part is used to send also the actual file name
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(),
                requestFile);
        //        RequestBody fileType = RequestBody.create(MultipartBody.FORM, FileType
        // .KTP_PHOTO.name().getBytes());
        // finally, execute the request

        final FileUploadApi service = ServiceGenerator.createService(FileUploadApi.class);
        final Call<ResponseBody> call = service.uploadVideo(body, loanAppId, fileType.name(),
                token);

        return Observable.create(subscriber -> call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call1,
                                   Response<ResponseBody> response) {
                XLog.INSTANCE.i("Upload video success", 10);
                subscriber.onNext(new Pair<>(call1, response));
            }

            @Override
            public void onFailure(Call<ResponseBody> call1, Throwable t) {
                XLog.INSTANCE.e("Upload video error:", t, 100);
                subscriber.onError(new UploadFileException(call1, t));

            }
        }));
    }

    public static Observable<Pair<Call<ResponseBody>, Response<ResponseBody>>> uploadVideoFile
            (FileType type, String path, String token, String loanAppId) {

        File f = new File(path);
        return uploadVideoFile(type, f, token, loanAppId);
    }

    public static Observable<Pair<Call<ResponseBody>, Response<ResponseBody>>> uploadPhotoFile
            (FileType fileType, String path, String token) {

        File f = new File(path);
        return uploadPhotoFile(fileType, f, token);
    }

    public static Observable<OcrResultBean> idOcr(@Nullable File file, @Nullable String token) {
        MediaType mediaType = MediaType.parse(FileUtils.getMimeType(file));
        RequestBody requestFile = RequestBody.create(mediaType, file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(),
                requestFile);
        final FileUploadApi service = ServiceGenerator.createService(FileUploadApi.class);
        return service.idOcr(body, token);
    }

    public static class UploadFileException extends Throwable {
        Call<ResponseBody> call;

        public UploadFileException(Call<ResponseBody> call, Throwable t) {
            super(t);
            this.call = call;
        }

        public Call<ResponseBody> getCall() {
            return call;
        }
    }
}
